<template>
    <div class="container-content">
        <div class="container-content-title">{{ $t('platform.setting.config.title') }}-config配置
            <el-button type="text" class="p-0" @click="dialogUpdate" v-if="dataStatus == 'dle'">{{ $t('action.edit') }}</el-button>
        </div>
        <component-page-loading :status="dataStatus" v-if="['loading', 'error'].indexOf(dataStatus) >= 0" @reload="getData"></component-page-loading>
        <el-form label-width="180px" class="detail" v-else>
            <el-col :md="12">
                <el-form-item :label="$t('platform.config.rtpConfig.rtpPeriodInitGain')">
                    {{ data.config.rtp_period.init.gain }}
                </el-form-item>
            </el-col>

            <el-col :md="12">
                <el-form-item :label="$t('platform.config.rtpConfig.rtpPeriodInitCost')">
                    {{ data.config.rtp_period.init.cost }}
                </el-form-item>
            </el-col>

            <el-col :md="12">
                <el-form-item :label="$t('platform.config.rtpConfig.rtpPeriodRtpExpect')">
                    {{ data.config.rtp_period.rtp_expect }}
                </el-form-item>
            </el-col>

            <el-col :md="12">
                <el-form-item :label="$t('platform.config.rtpConfig.rtpPeriodResetTime')">
                    {{ data.config.rtp_period.reset_time }}
                </el-form-item>
            </el-col>

            <el-col :md="12">
                <el-form-item :label="$t('platform.config.rtpConfig.rtpPeriodAddTime')">
                    {{ data.config.rtp_period.add_time }}
                </el-form-item>
            </el-col>

            <el-col :md="12">
                <el-form-item :label="$t('platform.config.rtpConfig.rtpPeriodSubTime')">
                    {{ data.config.rtp_period.sub_time }}
                </el-form-item>
            </el-col>

            <el-col :md="12">
                <el-form-item :label="$t('platform.config.rtpConfig.rtpPeriodPeriodTime')">
                    {{ data.config.rtp_period.period_time }}
                </el-form-item>
            </el-col>

            <el-col :md="12">
                <el-form-item :label="$t('platform.config.rtpConfig.rtpPeriodLastDuration')">
                    {{ data.config.rtp_period.last_duration }}
                </el-form-item>
            </el-col>

            <el-col :md="12">
                <el-form-item :label="$t('platform.config.rtpConfig.personReviseInitCost')">
                    {{ data.config.person_revise.init.cost }}
                </el-form-item>
            </el-col>

            <el-col :md="12">
                <el-form-item :label="$t('platform.config.rtpConfig.personReviseInitGain')">
                    {{ data.config.person_revise.init.gain }}
                </el-form-item>
            </el-col>

            <el-col :md="12">
                <el-form-item :label="$t('platform.config.rtpConfig.personReviseAddRtp')">
                    {{ data.config.person_revise.add_rtp }}
                </el-form-item>
            </el-col>

            <el-col :md="12">
                <el-form-item :label="$t('platform.config.rtpConfig.personReviseAddRtpRate')">
                    {{ data.config.person_revise.add_rtp_rate }}
                </el-form-item>
            </el-col>

            <el-col :md="12">
                <el-form-item :label="$t('platform.config.rtpConfig.personReviseSubRtp')">
                    {{ data.config.person_revise.sub_rtp }}
                </el-form-item>
            </el-col>

            <el-col :md="12">
                <el-form-item :label="$t('platform.config.rtpConfig.personReviseSubRtpRate')">
                    {{ data.config.person_revise.sub_rtp_rate }}
                </el-form-item>
            </el-col>

        </el-form>
        <el-dialog :title="$t('platform.setting.config.dialog.update')" width="680px" :visible.sync="dialog.visible.update">
            <div class="pr-12" v-if="dataCache.config != null">
                <el-form :model="dataCache" label-width="auto">
                    <el-row>
                        <el-col :md="12">
                            <el-form-item :label="$t('platform.config.rtpConfig.rtpPeriodInitGain')">
                                <el-input class="small" v-model.number="dataCache.config.rtp_period.init.gain" type="number"></el-input>
                            </el-form-item>
                        </el-col>

                        <el-col :md="12">
                            <el-form-item :label="$t('platform.config.rtpConfig.rtpPeriodInitCost')">
                                <el-input class="small" v-model.number="dataCache.config.rtp_period.init.cost" type="number"></el-input>
                            </el-form-item>
                        </el-col>

                        <el-col :md="12">
                            <el-form-item :label="$t('platform.config.rtpConfig.rtpPeriodRtpExpect')">
                                <el-input class="small" v-model.number="dataCache.config.rtp_period.rtp_expect" type="number"></el-input>
                            </el-form-item>
                        </el-col>

                        <el-col :md="12">
                            <el-form-item :label="$t('platform.config.rtpConfig.rtpPeriodResetTime')">
                                <el-input class="small" v-model.number="dataCache.config.rtp_period.reset_time" type="number"></el-input>
                            </el-form-item>
                        </el-col>

                        <el-col :md="12">
                            <el-form-item :label="$t('platform.config.rtpConfig.rtpPeriodAddTime')">
                                <el-input class="small" v-model.number="dataCache.config.rtp_period.add_time" type="number"></el-input>
                            </el-form-item>
                        </el-col>

                        <el-col :md="12">
                            <el-form-item :label="$t('platform.config.rtpConfig.rtpPeriodSubTime')">
                                <el-input class="small" v-model.number="dataCache.config.rtp_period.sub_time" type="number"></el-input>
                            </el-form-item>
                        </el-col>

                        <el-col :md="12">
                            <el-form-item :label="$t('platform.config.rtpConfig.rtpPeriodPeriodTime')">
                                <el-input class="small" v-model.number="dataCache.config.rtp_period.period_time" type="number"></el-input>
                            </el-form-item>
                        </el-col>

                        <el-col :md="12">
                            <el-form-item :label="$t('platform.config.rtpConfig.rtpPeriodLastDuration')">
                                <el-input class="small" v-model.number="dataCache.config.rtp_period.last_duration" type="number"></el-input>
                            </el-form-item>
                        </el-col>

                        <el-col :md="12">
                            <el-form-item :label="$t('platform.config.rtpConfig.personReviseInitCost')">
                                <el-input class="small" v-model.number="dataCache.config.person_revise.init.cost" type="number"></el-input>
                            </el-form-item>
                        </el-col>

                        <el-col :md="12">
                            <el-form-item :label="$t('platform.config.rtpConfig.personReviseInitGain')">
                                <el-input class="small" v-model.number="dataCache.config.person_revise.init.gain" type="number"></el-input>
                            </el-form-item>
                        </el-col>

                        <el-col :md="12">
                            <el-form-item :label="$t('platform.config.rtpConfig.personReviseAddRtp')">
                                <el-input class="small" v-model.number="dataCache.config.person_revise.add_rtp" type="number"></el-input>
                            </el-form-item>
                        </el-col>

                        <el-col :md="12">
                            <el-form-item :label="$t('platform.config.rtpConfig.personReviseAddRtpRate')">
                                <el-input class="small" v-model.number="dataCache.config.person_revise.add_rtp_rate" type="number"></el-input>
                            </el-form-item>
                        </el-col>

                        <el-col :md="12">
                            <el-form-item :label="$t('platform.config.rtpConfig.personReviseSubRtp')">
                                <el-input class="small" v-model.number="dataCache.config.person_revise.sub_rtp" type="number"></el-input>
                            </el-form-item>
                        </el-col>

                        <el-col :md="12">
                            <el-form-item :label="$t('platform.config.rtpConfig.personReviseSubRtpRate')">
                                <el-input class="small" v-model.number="dataCache.config.person_revise.sub_rtp_rate" type="number"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </div>
            <div slot="footer">
                <el-button @click="dialog.visible.update = false">{{ $t('action.cancel') }}</el-button>
                <el-button type="primary" @click="onSubmit">{{ $t('action.save') }}</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: "PlatformRtpConfig",
    props: ['tree'],
    data() {
        return {
            currencyListStatus: 'dle',
            currencyList: [],
            currencyCache: {
                real_currency: '',
                multiple: null,
            },
            currencyInit: {},
            currencyRefresh: false,
            dataStatus: 'dle',
            data: {
                plat_code: null,
                config: {
                    rtp_period: {
                        init: {
                            gain: null,
                            cost: null
                        },
                        rtp_expect: null,
                        reset_time: null,
                        add_time: null,
                        sub_time: null,
                        period_time: null,
                        last_duration: null,
                    },
                    person_revise: {
                        init: {
                            gain: null,
                            cost: null,
                        },
                        add_rtp: null,
                        add_rtp_rate: null,
                        sub_rtp: null,
                        sub_rtp_rate: null,
                    }
                }
            },
            dataEditStatus: false,
            dataCache: {},
            dataInit: {},
            dialog: {
                visible: {
                    update: false,
                }
            },
            msg: {
                code: 200,
                message: '',
                errors: {},
            },
        }
    },
    watch: {
        tree: {
            deep: true,
            immediate: true,
            handler(n, o) {
                // 获取数据
                n.id != null && this.getData();
            }
        },
    },
    created() {
        // 备份初始数据
        this.currencyInit = _.cloneDeep(this.currencyCache);
        this.dataInit = _.cloneDeep(this.data);
    },
    methods: {
        // 获取币种列表
        getCurrencyList() {
            this.currencyListStatus = 'loading';
            axios.get('/common/getCurrencyList', {
                params: {system: true}
            }).then((response) => {
                if (response.data.resp_msg.code == 200) {
                    this.currencyList = response.data.resp_data;
                    this.currencyListStatus = 'dle';
                } else {
                    this.currencyListStatus = 'error';
                }
            })
        },
        // 获取数据
        getData() {
            this.dataStatus = 'loading';
            axios.get('/platform/setting/config/getData', {
                params: {
                    plat_code: this.tree.code
                }
            }).then((response) => {
                if (response.data.resp_msg.code == 200) {
                    this.data = response.data.resp_data;
                    if (_.isEmpty(this.data.config)){
                        this.data = _.cloneDeep(this.dataInit);
                    }
                    console.log(this.data)
                    this.dataStatus = 'dle';
                } else {
                    this.dataStatus = 'error';
                }
            })
        },

        initMsg() {
            this.msg.code = 200;
            this.msg.message = '';
            this.msg.errors = {};
        },
        // 显示模态框（编辑）
        dialogUpdate() {
            this.initMsg();
            // 缓存数据
            this.currencyCache = _.cloneDeep(this.currencyInit);
            if (_.isEmpty(this.data.config)) {
                this.dataCache = _.cloneDeep(this.dataInit);
            } else {
                this.dataCache = _.cloneDeep(this.data);
            }
            // 平台code
            this.dataCache.plat_code = this.tree.code;
            // 显示模态框
            this.dialog.visible.update = true;
        },
        onSubmit() {
            this.initMsg();
            // loading 状态 start
            let loading = this.$loading();
            // 保存数据
            axios.post('/platform/setting/config/updateOrCreateConfig', this.dataCache).then((response) => {
                // loading 状态 close
                loading.close();
                // 逻辑处理
                if (response.data.resp_msg.code == 200) {
                    this.$message({
                        type: 'success',
                        message: this.$t('messages.succeeded', {status: this.$t('action.edit')}),
                        showClose: true
                    });
                    // 更新数据
                    this.data = response.data.resp_data;
                    // 关闭模态框
                    this.dialog.visible.update = false;
                    // 广播事件到父组件
                    this.$emit('update', this.data);
                } else if (response.data.resp_msg.code == 42000) {
                    this.$message({
                        type: 'error',
                        message: response.data.resp_msg.message,
                        showClose: true
                    });
                    this.msg = response.data.resp_msg;
                } else {
                    this.$message({
                        type: 'error',
                        dangerouslyUseHTMLString: true,
                        message: this.$t('messages.failed', {status: this.$t('action.edit')}) + `<div class="mt-2">${response.data.resp_msg.message}</div>`,
                        showClose: true
                    });
                }
            })
        },
    }
}
</script>

<style scoped>
::v-deep .el-dialog__body {
    padding-top: 15px;
    padding-bottom: 15px;
}
</style>
